package ui;

/**
 * JPanel HandleCustomers
 * 
 * @author XA9
 * 
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import db.BankDatabase;
import db.Customer;

public class HandleCustomers extends JPanel {
	private JLabel lblEmployeeMenu;
	private JLabel lblXA9;
	private JLabel lblLogo;
	private JButton btnDelete;
	private JButton btnAddNewCustomer;
	private JButton btnReturn;
	private JTable tblCustomers;
	private JScrollPane scpAccounts;
	private DefaultTableModel tableModel;
	private JLabel lblPleaseSelectAn;
	/** bankDatabase */
	private BankDatabase bankDatabase;
	/** table accountId */
	private String accountId = "";

	/**
	 * Create the panel.
	 */
	public HandleCustomers() {

		this.bankDatabase = new BankDatabase();

		setBackground(UIManager.getColor("Label.background"));
		setLayout(null);

		lblEmployeeMenu = new JLabel("Customer administration");
		lblEmployeeMenu.setOpaque(true);
		lblEmployeeMenu.setBackground(UIManager.getColor("Label.background"));
		lblEmployeeMenu.setHorizontalAlignment(SwingConstants.CENTER);
		lblEmployeeMenu.setForeground(Color.BLACK);
		lblEmployeeMenu.setFont(new Font("Eurostile", Font.BOLD, 22));
		lblEmployeeMenu.setBounds(0, 94, 735, 28);
		add(lblEmployeeMenu);

		lblXA9 = new JLabel("\u00A92013 CBS HA.IT (Gruppe XA-9)\t\t ");
		lblXA9.setForeground(UIManager.getColor("Button.darkShadow"));
		lblXA9.setBackground(UIManager.getColor("Button.darkShadow"));
		lblXA9.setHorizontalAlignment(SwingConstants.CENTER);
		lblXA9.setFont(new Font("Eurostile", Font.PLAIN, 11));
		lblXA9.setBounds(0, 550, 735, 50);
		add(lblXA9);

		btnDelete = new JButton("Delete");
		btnDelete.setFont(new Font("Dialog", Font.BOLD, 11));
		btnDelete.setBounds(74, 324, 185, 50);
		add(btnDelete);

		btnAddNewCustomer = new JButton("Add a new customer");
		btnAddNewCustomer.setFont(new Font("Dialog", Font.BOLD, 11));
		btnAddNewCustomer.setBounds(271, 324, 185, 50);
		add(btnAddNewCustomer);

		btnReturn = new JButton("Return to main menu");
		btnReturn.setForeground(UIManager.getColor("CheckBox.select"));
		btnReturn.setFont(new Font("Eurostile", Font.PLAIN | Font.BOLD, 13));
		btnReturn.setBounds(74, 380, 185, 45);
		add(btnReturn);

		lblLogo = new JLabel("");
		lblLogo.setIcon(new ImageIcon(
				"/Users/martinotto-sidelmann/Desktop/Zombium.png"));
		lblLogo.setBounds(530, 12, 200, 70);
		add(lblLogo);

		scpAccounts = new JScrollPane();
		scpAccounts.setBounds(77, 152, 582, 160);
		scpAccounts.setFont(new Font("Eurostile", Font.PLAIN, 13));
		add(scpAccounts);

		this.tableModel = new DefaultTableModel()
		  {
		    public boolean isCellEditable(int row, int column)
		    {
		      return false;
		    }
		  };

		tblCustomers = new JTable(this.tableModel);
		tblCustomers.setRowSelectionAllowed(true);
		ListSelectionModel cellSelectionModel = tblCustomers
				.getSelectionModel();
		cellSelectionModel
				.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		cellSelectionModel
				.addListSelectionListener(new ListSelectionListener() {
					public void valueChanged(ListSelectionEvent e) {
						if (e.getValueIsAdjusting()) {
							return;
						}

						int[] selectedRow = tblCustomers.getSelectedRows();
						int[] selectedColumns = tblCustomers
								.getSelectedColumns();

						if (selectedRow.length <= 0) {
							return;
						}

						String selectedData = tblCustomers.getValueAt(
								selectedRow[0], 0).toString();
						accountId = selectedData;
						System.out.println("Selected: " + selectedData);
					}

				});

		this.tableModel.addColumn("LoginId");
		this.tableModel.addColumn("Password");
		this.tableModel.addColumn("CPR");
		this.tableModel.addColumn("Firstname");
		this.tableModel.addColumn("Lastname");

		scpAccounts.setViewportView(tblCustomers);
		tblCustomers.setBackground(Color.WHITE);
		tblCustomers.setRowHeight(25);
		tblCustomers.setFont(new Font("Eurostile", Font.PLAIN, 11));
		tblCustomers.getTableHeader();
		tblCustomers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		lblPleaseSelectAn = new JLabel(
				"Please select a customer before pressing a button ");
		lblPleaseSelectAn.setHorizontalAlignment(SwingConstants.CENTER);
		lblPleaseSelectAn.setForeground(Color.BLACK);
		lblPleaseSelectAn.setFont(new Font("Eurostile", Font.PLAIN, 14));
		lblPleaseSelectAn.setBounds(0, 124, 735, 16);
		add(lblPleaseSelectAn);

	}

	/**
	 * Display customers from database in JTable
	 * 
	 */
	public void displayCustomers() {

		this.tableModel.setRowCount(0);

		ArrayList<Customer> customers = this.bankDatabase.getCustomers();

		for (Customer customer : customers) {
			Object[] data = { customer.getLoginId(), customer.getPassword(),
					customer.getCpr(), customer.getFirstName(),
					customer.getLastName() };
			this.tableModel.addRow(data);
		}
	}

	/**
	 * Returns value of selected rows accountId
	 * 
	 * @return String accountId
	 */
	public String getAccountId() {
		return this.accountId;
	}

	/**
	 * Sets value of selected rows accountId 
	 * flush accountId value
	 * 
	 * @return String accountId
	 */
	public String setAccountId() {
		return accountId = "";
	}

	/**
	 * Returns JButton
	 * 
	 * @return btnDelete
	 */
	public JButton getBtnDelete() {
		return btnDelete;
	}

	/**
	 * Returns JButton
	 * 
	 * @return btnAddNewCustomer
	 */
	public JButton getBtnAddNewCustomer() {
		return btnAddNewCustomer;
	}

	/**
	 * Returns JButton
	 * 
	 * @return btnReturn
	 */
	public JButton getBtnReturn() {
		return btnReturn;
	}

	/**
	 * Add ActionListener to JButtons
	 * 
	 * @param ActionListener
	 *            l
	 * 
	 */
	public void addActionListener(ActionListener l) {
		btnDelete.addActionListener(l);
		btnAddNewCustomer.addActionListener(l);
		btnReturn.addActionListener(l);

	}
}
